/* << fastinsert report library >> ReportDemo.cpp Oct 30, 2010
 *
 * 	http://code.google.com/p/libfir
 *
 * Copyright (c) 2005 Pavlo Drobov (drobov.pavel@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


#include <string.h>
#include <stdio.h>

#include "PdfBase.h"
#include "PdfGrid.h"
#include "ExcelBase.h"
#include <argtable2.h>

#define GRID_QNT 	2
#define COLUMN_QNT 	4
#define ROW_QNT 	100

using namespace dasco::report;

class Grid: public GridBase
{
public:
	Grid(unsigned qnt = 0, const char* outline = "demo grid")
		:GridBase(outline), qnt(qnt)
	{
		make_trash_grid();
	}

	virtual ~Grid()
	{
	}

private:
	char buf[0xFF];
	unsigned qnt;
	void make_trash_grid()
	{
		IRowSetHeader * rowSetHeader = new RowSetHeader;
		//add rownumber column
		rowSetHeader->addColumn("rownum");
		for (unsigned j = 0; j < COLUMN_QNT; ++j)
		{
			::sprintf(buf, "header %i", j);
			rowSetHeader->addColumn(buf);
		}
		addRow(rowSetHeader);

		for (unsigned r = 0; r < ROW_QNT; ++r)
		{
			IRowSet * rowSet = new RowSet;
			//add rownumber
			rowSet->addColumn((int) r);
			for (unsigned c = 0; c < COLUMN_QNT; ++c)
			{
				::sprintf(buf, "cell(%i [%i/%i])", qnt,r,c);
				rowSet->addColumn(buf);
			}
			addRow(rowSet);
		}
	}
};

template<class T>
class Report: public T
{
public:
	Report()
	{
		make_demo();
	}
	virtual ~Report(){};
private:
	void make_demo()
	{
		char buf[0xFF];
		for (unsigned i = 0; i < GRID_QNT; ++i)
		{
			::sprintf(buf, "grid %i", i);
			Grid * grid = new Grid(i, buf);
			T::renderGrid(grid);
			delete grid;
		}
	}
};

class Excel: public ExcelBase
{
public:
	Excel()
	{
		make_demo();
	}

	virtual ~Excel()
	{
	}

private:
	void make_demo()
	{
		Grid * grid = new Grid();
		renderGrid(grid);
		delete grid;
	}
};

class Pdf : public Report<PdfBase>
{
};

static void make_pdf_demo(const char * savePathDir)
{
	//Pdf * pdf = new Pdf;
	Report<PdfBase> * pdf = new Report<PdfBase>;
	pdf->numerationPage("Page");
	pdf->currentDate();
	pdf->setFIRdemo();

	char buf[0x1FF];
	::sprintf(buf,"%s/out.pdf",savePathDir);
	try
	{
		pdf->save(buf);
	} catch (IIOException& ex)
	{
		::printf("IO Error: %i \n", (errorNumber) ex);
	}
	delete pdf;
}

static void make_excel_demo(const char * savePathDir)
{
	Report<ExcelBase> * excel = new Report<ExcelBase>;

	char buf[0x1FF];
	::sprintf(buf,"%s/out.xls",savePathDir);
	try
	{
		excel->save(buf);
	} catch (IIOException& ex)
	{
		::printf("IO Error: %i \n", (errorNumber) ex);
	}

	delete excel;
}


int main(int argc, char **argv)
{
	struct arg_lit *help = arg_lit0(NULL, "help", "print this help and exit");
	struct arg_str *out_dir = arg_str0("o", "out_dir", NULL, "output dir (default .)");
	struct arg_end *end = arg_end(20);

	void* argtable[] = { help, out_dir,end };

	if (::arg_parse(argc, argv, argtable) > 0)
	{
		::arg_print_errors(stdout, end, argv[0]);
		::printf("Try '%s --help' for more information.\n", argv[0]);
		return -1;
	}

    if (argc ==1 || help->count > 0)
    {
        ::arg_print_syntax(stdout,argtable,"\n");
        ::arg_print_glossary(stdout,argtable,"  %-25s %s\n");
        ::arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
        return -1;
    }


	make_pdf_demo(out_dir->count ? out_dir->sval[0] : ".");
	make_excel_demo(out_dir->count ? out_dir->sval[0] : ".");

	::arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
	return 0;
}
